//Page Object
import {
    request
} from "../../request/index.js"
Page({
    data: {
        taps: [{
            id: 0,
            value: "综合",
            isActive: true
        },
        {
            id: 1,
            value: "销量",
            isActive: false
        },
        {
            id: 2,
            value: "价格",
            isActive: false
        }
        ],
        goodslist: [],
        total: 0,
    },
    //接口参数
    QueryParams: {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10
    },

    //options(Object)
    onLoad: function (options) {

        this.QueryParams.cid = options.cid || ""
        this.QueryParams.query = options.query || ""
        this.getGoodsList()
    },
    onReady: function () {

    },
    onShow: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    //下拉刷新
    onPullDownRefresh: function () {
        this.setData({
            goodslist: []
        });
        this.QueryParams.pagenum = 1;
        this.getGoodsList();

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {

    },
    onPageScroll: function () {

    },
    //item(index,pagePath,text)
    onTabItemTap: function (item) {

    },
    async getGoodsList() {

        const res = await request({
            url: '/goods/search',
            data: this.QueryParams
        })
        const total = Math.ceil(res.data.message.total / this.QueryParams.pagesize)
        let goodslist = res.data.message.goods.filter(v => {
            return v.goods_small_logo != ''
        })
        this.setData({
            goodslist: [...this.data.goodslist, ...goodslist],
            total
        })

        //关闭下拉刷新
        wx.stopPullDownRefresh();
    },




    handleTapsItemChange(e) {
        let index = e.detail.index
        let {
            taps
        } = this.data
        taps.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        this.setData({
            taps
        })
    },
    // 滚动条触底事件
    onReachBottom() {
        if (this.QueryParams.pagenum >= this.data.total) {
            wx.showToast({
                title: '没有下一页数据',
            });

        } else {
            this.QueryParams.pagenum++;
            this.getGoodsList()
        }
    }
});